// index.js
const request = require("../../utils/request1.js")
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    // 判断value值
    radioValue:null,
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  onChange(event) {
    console.log('radio', event.detail);
  },
  // 登录接口
  jum() {
    if(this.data.radioValue !=1){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return false
    }
    wx.login({
      success: (res) => {
        wx.request({
          url: 'http://120.46.22.142/login',
          method:'POST',
          data:{
            code:res.code,
            appid:'wx3efedd9b292d764a',
            secret:'41a0767929600a8102f5979bb429dbd3'
          },
          success(result){
            if(result.data.code ==0){
              // 处理错误
            }else{
              wx.setStorageSync('token', result.data.data.token)
              wx.switchTab({
                url: '/pages/index/index'
              })
              console.log(result.data.data.token);
            }
            // console.log(result);
          }
        })
        console.log(res.code);
      },
    })
 
   
  },
  // 获取value
  getValue(e){
    console.log(e);
    this.setData({
      radioValue:e.detail.value
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
